package com.pets.service.Impl;

import com.pets.dao.ApplyMapper;
import com.pets.entity.Apply;
import com.pets.enums.YesNoEnum;
import com.pets.qo.ApplyQO;
import com.pets.service.ApplyService;
import com.pets.util.Result;
import com.pets.util.UploadUtil;
import com.pets.vo.UserApplyVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class ApplyServiceImpl implements ApplyService {

    @Resource
    private ApplyMapper applyMapper;

    @Override
    public List<UserApplyVO> getUserApplyList(int uId) {
        return applyMapper.getUserApplyList(uId);
    }

    @Override
    public Result doApply(ApplyQO applyQO) {
        Result result=new Result();
        //做文件上传动作
        String url ;
        try {
            url = UploadUtil.uploadFile(applyQO.getFile());
        } catch (IOException e) {
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("文件上传失败");
            e.printStackTrace();
            return result;
        }
        Apply apply = new Apply();
        apply.setUid(applyQO.getuId());
        apply.setAppreason(applyQO.getAppReason());
        apply.setAppimgeurl(url);
        apply.setAppstatus("0");
        apply.setAppcurrenttime(new Date());
        int i = applyMapper.insert(apply);
        if(i >0){
            result.setCode(YesNoEnum.YES.getCode());
            result.setMsg("提交申请成功");
            return result;
        }else {
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("提交申请失败");
            return result;
        }
    }

    @Override
    public Result cancelApply(int appId) {
        Result result = new Result();
        Apply apply = applyMapper.selectByPrimaryKey(appId);
        apply.setAppstatus("3");
        int i = applyMapper.updateByPrimaryKey(apply);
        if(i >0){
            result.setCode(YesNoEnum.YES.getCode());
            result.setMsg("取消申请成功");
            return result;
        }else {
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("取消申请失败");
            return result;
        }
    }

    @Override
    public UserApplyVO getUserApplyingByuId(int uId) {
        return applyMapper.getUserApplyingByuId(uId);
    }
}
